﻿//#define firstRelease


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
using Foundation;
using RPGSoundmixer.SoundSuiteExplorer;
using Business;


namespace RPGSoundmixer {
    public partial class Configuration : Form {
        private string[] cmbKeyCatList = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        private string[] cmbKeyEffectList = new string[] { "a", "b", "c", "d", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };


        private CategorieCollection categories = CategorieCollection.GetInstance();
        private KeyHandler handler;
        private IDataHandler dataHandler; 

        public Configuration() {
            InitializeComponent();
            this.dataHandler = DataHandler.GetInstance();
            this.dataHandler.PropertyChanged += new PropertyChangedEventHandler(dataHandler_PropertyChanged);
        }

        void dataHandler_PropertyChanged(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName.Equals("CurrentSoundSuite")) {
                loadCurrentSoundSuite();
            }

        }

        /// <summary>
        /// Lädt die Werte der aktuellen SoudSuite in die View
        /// </summary>
        private void loadCurrentSoundSuite() {
            this.lstCategorie.Items.AddRange(this.dataHandler.GetCurrentEffectGroups()); 
        }

        public Configuration(KeyHandler handler)
            : this() {
            this.handler = handler;            
            this.loadCategories();
        }

        private void loadCategories() {
            this.lstCategorie.Items.Clear();
            this.cmbCatIdx.Items.Clear();
            this.cmbCatIdx.Items.AddRange(this.cmbKeyCatList);

            foreach (KeyValuePair<char, EffektGroup> pair in this.handler.CategorieCollection.Categories) {
                this.lstCategorie.Items.Add(pair.Value);
                this.removeKeyFromCombobox(pair.Key, this.cmbCatIdx);
            }
        }

        private void label2_Click(object sender, EventArgs e) {

        }

        private void button3_Click(object sender, EventArgs e) {
            
        }

        /// <summary>
        /// Entfernt einen Key aus der mitgegebenen Combobox
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cmb"></param>
        private void removeKeyFromCombobox(char key, ComboBox cmb) {
            bool isRemoved = false;
            for (int i = 0; i < cmb.Items.Count && !isRemoved; i++) {
                if (cmb.Items[i].ToString().Equals(key.ToString())) {
                    cmb.Items.RemoveAt(i);
                    isRemoved = true;
                }
            }
        }

        private void lstCategorie_SelectedIndexChanged(object sender, EventArgs e) {
            this.refreshEffectLIst();
        }


        private void refreshEffectLIst() {
            lstEffect.Items.Clear();
            cmdEffect.Items.Clear();
            cmdEffect.Items.AddRange(this.cmbKeyEffectList);
            if (this.lstCategorie.SelectedItem != null) {                
                foreach (KeyValuePair<char, Effekt> effectPair in ((EffektGroup)lstCategorie.SelectedItem).Effects) {
                    this.lstEffect.Items.Add(effectPair.Value);
                    this.removeKeyFromCombobox(effectPair.Key, this.cmdEffect);
                }
            }
            this.refreshTrackBox();
        }

        private void refreshTrackBox() {
            lstTrack.Items.Clear();
            if (lstEffect.SelectedItem != null) {

                foreach(Track t in ((Effekt)((ListBox)lstEffect).SelectedItem).Tracks){                
                    this.lstTrack.Items.Add(t);
                }
            }
        }

        private void lstEffect_SelectedIndexChanged(object sender, EventArgs e) {
            refreshTrackBox();
        }

        private void button1_Click(object sender, EventArgs e) {
#if firstRelease
            if (this.cmbCatIdx.SelectedItem != null && !string.IsNullOrEmpty(this.cmbCatIdx.SelectedItem.ToString())) {
                EffektGroup group = new EffektGroup();
                group.Name = this.txtKatName.Text;
                group.Key = this.cmbCatIdx.Text.ToCharArray()[0];
            }
            
#else

            if (this.cmbCatIdx.SelectedItem != null && !string.IsNullOrEmpty(this.cmbCatIdx.SelectedItem.ToString())) {
                EffektGroup newCat = new EffektGroup();
                newCat.Name = this.txtKatName.Text;
                newCat.Key = this.cmbCatIdx.SelectedItem.ToString().ToCharArray()[0];
                this.handler.CategorieCollection.Categories.Add(newCat.Key, newCat);
                this.cmbCatIdx.Items.RemoveAt(this.cmbCatIdx.SelectedIndex);
                this.loadCategories();
            }
#endif
        }


        private void button2_Click(object sender, EventArgs e) {
            if (lstCategorie.SelectedItem == null) {
                MessageBox.Show("Es wurde keine Kategorie ausgewählt");
            }
            else {
                if (!string.IsNullOrEmpty(this.cmdEffect.SelectedItem.ToString())) {
                    Effekt newEffekt = new Effekt();
                    newEffekt.Name = this.txtEffekt.Text;
                    newEffekt.Key = this.cmdEffect.SelectedItem.ToString().ToCharArray()[0];
                    newEffekt.Volume = 100;

                    ((EffektGroup)lstCategorie.SelectedItem).Effects.Add(newEffekt.Key, newEffekt);
                    this.cmdEffect.Items.RemoveAt(this.cmdEffect.SelectedIndex);
                    this.refreshEffectLIst();
                }
            }
        }

        private void button7_Click(object sender, EventArgs e) {
            if (lstEffect.SelectedItem == null) {
                MessageBox.Show("Es wurde kein Effekt ausgewählt");
            }
            else {
                EffektControl ec = new EffektControl((Effekt)this.lstEffect.SelectedItem);
                ec.ShowDialog(); 
                this.refreshTrackBox();
            }
        }

        private void button9_Click(object sender, EventArgs e) {
            SaveFileDialog dialog = new SaveFileDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                new LoadSave().SaveMusicSuite(this.handler.CategorieCollection, dialog.FileName);
            }
        }

        private void button8_Click(object sender, EventArgs e) {
            //SoundSuiteExplorer.SoundSuiteExplorerView soundSuiteExplorerDialog = new SoundSuiteExplorerView();
            //soundSuiteExplorerDialog.ShowDialog(); 

            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                LoadSave o = new LoadSave();
                this.handler.CategorieCollection = o.LoadMusicSuite(dialog.FileName);
                this.loadCategories();
            }
        }

        private void button5_Click(object sender, EventArgs e) {
            if (((ListBox)sender).SelectedItem != null) {
                this.categories.Categories.Remove(((EffektGroup)lstCategorie.SelectedItem).Key);
                this.loadCategories();
                this.refreshEffectLIst();
            }
        }

        private void btEffDelete_Click(object sender, EventArgs e) {
            if (lstEffect.SelectedItem != null && lstCategorie.SelectedItem != null) {
                ((EffektGroup)lstCategorie.SelectedItem).Effects.Remove(((Effekt)lstEffect.SelectedItem).Key);
                this.refreshEffectLIst();
            }
            else if (lstCategorie.SelectedItem == null) {
                MessageBox.Show("Es wurde keine Kategorie ausgewählt");
            }
            else {
                MessageBox.Show("Es wurde kein Effekt ausgewählt");
            }
        }

        private void button3_Click_1(object sender, EventArgs e) {
            List<string> list = new List<string>(); 
            List<Track> tracks = new List<Track>();
            
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK) {
                list.AddRange(openFileDialog1.FileNames);                
            }

            IDataAccess data = DataAccess.DataAccess.GetInstance(); 

            MessageBox.Show("Ech Liebe Dech"); 
            data.writeTracks(Track.GetTrackListFromFileList(list));

        }

        private void label1_Click(object sender, EventArgs e) {

        }

        

       
    }
}


